﻿using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace WpfRedis.Views
{
    /// <summary>
    /// TriageWebView.xaml 的交互逻辑
    /// </summary>
    public partial class WebView : UserControl
    {
        public WebView()
        {
            InitializeComponent();
            //https://blog.csdn.net/Naylor_5/article/details/128812289
        }

        private void webView_Loaded(object sender, RoutedEventArgs e)
        {
            string url = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test", "1.html");
            this.webView.Source = new Uri(url);
            this.webView.WebMessageReceived += Wv2_WebMessageReceived;
        }
        /// <summary>
        /// 收到网页调用的消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Wv2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
        {
            //接收到的字符串
            //string msg = e.TryGetWebMessageAsString();
            //接收到的json
            string msgJson = e.WebMessageAsJson;
            MessageBox.Show(msgJson);
        }
        /// <summary>
        /// C#调用webView页面方法
        /// </summary>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            webView.CoreWebView2.ExecuteScriptAsync("WpfMsg('123')");
        }
    }
}
